﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MBSDAL;




namespace MBSBusinessEntities

{
    public class ScreenBE
    {

        #region Constructors

        public ScreenBE()
        {
            mbs = new MBS_Entities();
        }

        public ScreenBE(int CinemaID, int ScreenNo, int NumOfSeats)
        {
            this.CinemaID = CinemaID;
            this.ScreenNo = ScreenNo;
            this.NumOfSeats = NumOfSeats;
            
            mbs = new MBS_Entities();
        }
        #endregion

        #region ClassVariables

        private MBS_Entities mbs;

        private int cinemaID;
              
        private int screenNo;
        
        private int numOfSeats;

        private bool isDeleted;

        private bool isValid =true;

        public bool IsValid
        {
            get { return isValid; }
            set { isValid = value; }
        }

        public static string ErrorMessage;

        #endregion


        #region Properties
        public int CinemaID
        {
            get { return cinemaID; }
            set { cinemaID = value; }
        }

        public int ScreenNo
        {
            get { return screenNo; }
            set { screenNo = value; }
        }

        public int NumOfSeats
        {
            get { return numOfSeats; }
            set { numOfSeats = value; }
        }

        public bool IsDeleted
        {
            get { return isDeleted; }
            set { isDeleted = value; }
        }
        #endregion


        #region Methods


        private bool Validate_Insertion()
        {
            if (this.CinemaID == 0) { isValid = false; return false; }
            if (this.ScreenNo == 0) { isValid = false; return false; }
            if (this.NumOfSeats == 0) { isValid = false; return false; }
            if (this.isDeleted == true) { isValid = false; return false; }
            return true;
        }


        public bool Add()
        {

            if (Validate_Insertion())
            {
            try
            {

                Screen existingScreen = GetScreenDALByID(this.CinemaID, this.ScreenNo);
                if (existingScreen == null)
                {

                    Screen screen = new Screen();
                    screen.CinemaID = this.CinemaID;
                    screen.ScreenNo = this.ScreenNo;
                    screen.NumOfSeats = this.NumOfSeats;
                    screen.IsDeleted = false;

                    mbs.AddToScreens(screen);
                    mbs.SaveChanges();
                }
                else
                {
                    existingScreen.IsDeleted = false;
                    mbs.SaveChanges();
                }
            }

            catch (System.Data.UpdateException ex)
            {
                //Duplicate ScreenNumber exists in the database.
                ErrorMessage = ex.Message;
                return false;
            }
             return true;
           
            }
            return false;
        }

        public bool Delete(int CinemaID,int ScreenNo)
        {
            Screen screen = GetScreenDALByID(CinemaID, ScreenNo);

            if (screen == null)
            {
                return false;
            }
            screen.IsDeleted = true;
            mbs.SaveChanges();


            return true;
        }

        public bool Validate_Retrieval(ScreenBE screenbe)
        {
            if (screenbe == null) { isValid = false; return false; }
            return true;
        }        
        
        public ScreenBE GetScreenByID(int CinemaID, int ScreenNo)
        {
            try
            {

                ScreenBE screenbe;
                Screen screen = GetScreenDALByID(CinemaID, ScreenNo);

                screenbe = ConvertDALToBusinessEntity(screen);

                if (Validate_Retrieval(screenbe))
                {
                    return screenbe;
                }

                else
                {
                    return null;
                }
            }

            catch (Exception ex)
            {
                ErrorMessage = ex.Message;
                return null;
            }
            
        }


        private ScreenBE ConvertDALToBusinessEntity(Screen screenDAL)
        {
            if (screenDAL == null) return null;
            ScreenBE screenbe = new ScreenBE(screenDAL.CinemaID,screenDAL.ScreenNo,Convert.ToInt32(screenDAL.NumOfSeats));
            
            return screenbe;
        }

        private Screen GetScreenDALByID(int CinemaID, int ScreenNo)
        {
            Screen screenDAL = new Screen();
            
            try
            {
            screenDAL = mbs.Screens.First(x => x.CinemaID == CinemaID && x.ScreenNo == ScreenNo);

             }
           catch (System.InvalidOperationException ex)
           {
               return null;
           }

            return screenDAL;

        }

        private List<Screen> GetAllDALScreens()
        {
            return mbs.Screens.ToList();
        }

        public List<ScreenBE> GetAllScreens()
        {

            List<Screen> dalscreenlist = GetAllDALScreens();

            return ListOperation(dalscreenlist);

        }

        private List<ScreenBE> ListOperation(List<Screen> dalscreenlist)
        {

            List<ScreenBE> screenlist = new List<ScreenBE>();

            foreach (Screen dalscreen in dalscreenlist)
            {
                ScreenBE screenbe = ConvertDALToBusinessEntity(dalscreen);
                if (screenbe != null)
                    screenlist.Add(screenbe);
            }

            return screenlist;
        }

        public List<ScreenBE> GetAllScreens_Cinema(int CinemaID)
        {
            List<Screen> dalscreenlist = GetAllDALScreens_Cinema(CinemaID);

            return ListOperation(dalscreenlist);
        }

        private List<Screen> GetAllDALScreens_Cinema(int CinemaID)
        {
            try
            {
                return mbs.Screens.Where(x => x.CinemaID == CinemaID && x.IsDeleted == false).ToList();
            }

            catch (Exception ex)
            {
                ErrorMessage = ex.Message;
                return new List<Screen>();
            }
        }

        #endregion

        #region MovieSchedule

        public bool AddSchedule(int MovieScheduleID, DateTime StartDate, DateTime EndDate, TimeSpan ShowStartTime, TimeSpan ShowEndTime, double WeekDateFare, double WeekEndFare, bool IsDeleted, int CinemaID, int ScreenNo, int MovieID)
        {
           
            MovieScheduleBE movieScheduleBE = new MovieScheduleBE(StartDate, EndDate, ShowStartTime, ShowEndTime, WeekDateFare, WeekEndFare, IsDeleted, this.CinemaID, this.ScreenNo, MovieID);

            return movieScheduleBE.Add();
                        
        }


        public bool DeleteSchedule()
        {
            MovieScheduleBE movieScheduleBE = new MovieScheduleBE();

            return movieScheduleBE.Delete();
        }


        #endregion
    }
}
